?><?
$file_tags=array();
class cls_filter_file extends cls_filter{
  function process_html($object,$html){
    global $file_tags;
    $file_tags=array();
    $html=preg_replace_callback("|\{file(.*?)\}|i","filter_collect_files",$html);
    foreach($file_tags as $key=>$tag){
      $tag_params=array();
      $idx=0;
      $token=strtok($tag,"=\"");
      $name="";
      $value="";
      do{
        if($idx++%2==0){
          $name=trim($token);
        }else{
          $tag_params[$name]=trim($token);
        }
      }while($token=strtok("=\""));
      $file_code=$this->insert_file($tag_params);
      $html = str_replace("%%FILE(".$key.")%%",$file_code,$html);
    }
    return $html;
  }

  function insert_file($tag_params){
    $id  =gav($tag_params,"id");
    $path=gav($tag_params,"path");
    $kind=gav($tag_params,"kind");
    $name=gav($tag_params,"name");
    $size=gav($tag_params,"size");
    $prev=gav($tag_params,"prev");
    $preview=gav($tag_params,"preview");
    $name=$name?$name:ExtractFileName($path);
    $size=$size?$size:isSmall;
    $result="";
    if($id!=""){
      $file=cls_file_lib::item($id);
      $path=$file->name;
    }
    if(file_exists($path)&&$kind){
      switch($kind){
      case fkLink : {$result="<a href='/".$path."'>$name</a>";break;}
      case fkIcon : {$result="<a href='/".$path."'><img src='/".get_file_icon($path,$size)."' /></a>";break;}
      case fkVideo: {$result=insert_video($path,$preview);break;}
      case fkFlash: {$result=insert_flash($id,$path);break;}
      case fkDown : {$result=insert_down ($id,$path,$name);break;}
      case fkPopup: {$result=insert_popup($id,$path,$name,$preview);break;}
      default: {$result="<a href='/".$path."'>$name</a>";break;}
      }
    }
    return $result;
  }
}

function filter_collect_files($matches){
  global $file_tags;
  $file_tags[]=$matches[1];
  return "%%FILE(".(count($file_tags)-1).")%%";
}



$filters[]="cls_filter_file";